package com.jiang.jetpackdemo.ui

import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.widget.ImageButton
import android.widget.ImageView
import com.jiang.jetpackdemo.R
import com.jiang.jetpackdemo.model.ImgInfo
import com.jiang.jetpackdemo.utils.loadPicFromNet

/**
 *
 * @ProjectName:    JetpackDemo
 * @ClassName:      ImageDialog
 * @Description:     java类作用描述
 * @Author:         江
 * @CreateDate:     2020/6/20 15:43
 */
class ImageDialog(private val imgInfo: ImgInfo, context: Context) :
    AlertDialog(context, R.style.BaseDialog) {
    private lateinit var btn_share: ImageButton
    private lateinit var btn_download: ImageButton
    private lateinit var btn_star: ImageButton
    private lateinit var imageView: ImageView
    private var shareOnClick: (() -> Unit?)? = null
    private var downloadOnClick: (() -> Unit?)? = null
    private var starOnClick: (() -> Unit?)? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.image_dialog)
        initView()
        initListener()
        loadImage()
    }

    private fun loadImage() {
        imageView.loadPicFromNet(imgInfo.imgurl)
    }

    private fun initListener() {
        downloadOnClick?.let { btn_download.setOnClickListener { it() } }
        shareOnClick?.let { btn_share.setOnClickListener { it() } }
        starOnClick?.let { btn_star.setOnClickListener { it() } }
    }

    private fun initView() {
        btn_download = findViewById<ImageButton>(R.id.btn_dialog_download)
        btn_share = findViewById<ImageButton>(R.id.btn_dialog_share)
        btn_star = findViewById<ImageButton>(R.id.btn_dialog_star)
        imageView = findViewById<ImageView>(R.id.iv_dialog)
    }

    fun setShareOnClickListener(onClick: () -> Unit): ImageDialog {
        shareOnClick = onClick
        return this
    }

    fun setDownloadOnClickListener(onClick: () -> Unit): ImageDialog {
        downloadOnClick = onClick
        return this
    }

    fun setStarOnClickListener(onClick: () -> Unit): ImageDialog {
        starOnClick = onClick
        return this
    }

}